# 创建基本的员工管理页面
<template>
  <div class="employee-container">
    <el-card class="employee-card">
      <div class="card-header">
        <div class="left">
          <span class="title">员工管理</span>
        </div>
        <div class="actions">
          <el-button type="primary" @click="handleAddEmployee">
            <el-icon><Plus /></el-icon>新增员工
          </el-button>
        </div>
      </div>

      <!-- 搜索表单 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="关键词">
          <el-input
            v-model="searchForm.keyword"
            placeholder="姓名/手机号/邮箱"
            clearable
            @keyup.enter="handleSearch"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 员工列表 -->
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column prop="name" label="姓名" min-width="120" />
        <el-table-column prop="phone" label="手机号" min-width="120">
          <template #default="{ row }">
            {{ maskPhone(row.phone) }}
          </template>
        </el-table-column>
        <el-table-column prop="email" label="邮箱" min-width="180">
          <template #default="{ row }">
            {{ maskEmail(row.email) }}
          </template>
        </el-table-column>
        <el-table-column prop="role" label="角色" min-width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'info'">
              {{ row.status === 1 ? "在职" : "离职" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          min-width="180"
          :formatter="row => formatDateTime(row.createTime)"
        />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button-group>
              <el-button link type="primary" @click="handleEditEmployee(row)">
                <el-icon><Edit /></el-icon>编辑
              </el-button>
              <el-button link type="danger" @click="handleDeleteEmployee(row)">
                <el-icon><Delete /></el-icon>删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="searchForm.page"
          v-model:page-size="searchForm.pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { formatDateTime } from "@/utils/date";
import { maskPhone, maskEmail } from "@/utils/tools";

// 状态定义
const loading = ref(false);
const total = ref(0);
const tableData = ref([]);

// 搜索表单
const searchForm = reactive({
  keyword: "",
  page: 1,
  pageSize: 10
});

// 加载数据
const loadData = async () => {
  loading.value = true;
  try {
    // TODO: 调用员工列表 API
    loading.value = false;
  } catch (error) {
    console.error("Failed to load employees:", error);
    ElMessage.error("加载员工数据失败");
    loading.value = false;
  }
};

// 搜索
const handleSearch = () => {
  searchForm.page = 1;
  loadData();
};

// 重置
const handleReset = () => {
  searchForm.keyword = "";
  searchForm.page = 1;
  loadData();
};

// 分页大小变化
const handleSizeChange = (val: number) => {
  searchForm.pageSize = val;
  loadData();
};

// 页码变化
const handleCurrentChange = (val: number) => {
  searchForm.page = val;
  loadData();
};

// 添加员工
const handleAddEmployee = () => {
  // TODO: 实现添加员工功能
};

// 编辑员工
const handleEditEmployee = (row: any) => {
  // TODO: 实现编辑员工功能
};

// 删除员工
const handleDeleteEmployee = (row: any) => {
  ElMessageBox.confirm("确定要删除该员工吗？删除后不可恢复！", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(async () => {
      // TODO: 调用删除 API
      ElMessage.success("删除成功");
      loadData();
    })
    .catch(() => {});
};

// 初始化
onMounted(() => {
  loadData();
});
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 24px;
  background: var(--el-bg-color-page);

  .employee-card {
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 24px;

      .left {
        .title {
          font-size: 18px;
          font-weight: 600;
          color: var(--el-text-color-primary);
        }
      }
    }

    .search-form {
      margin-bottom: 24px;
    }
  }

  .pagination-container {
    display: flex;
    justify-content: flex-end;
    margin-top: 24px;
  }
}
</style>
